﻿using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CloudStorageLight.Core
{
    public enum FileReciveMode
    {
        None,
        Current,
        User,
        Session,
    }


    public class FileTransferWorkTask : TableEntity, IWorkTask
    {
        public string Files { get; set; }

        public string Password { get; set; }

        public bool Bcc { get; set; }

        public string From { get; set; }

        public string To { get; set; }

        public string DownloadedUsers { get; set; }

        public string Title { get; set; }

        public string MailBody { get; set; }

        public DateTime Expired { get; set; }

        public DateTime CraetedDate { get; set; }

        public int? ReminderTimes { get; set; }

        public int ReminderCount { get; set; }

//        public string LinkAddress { get; set; }

        public string FileReciveMode { get; set; }

        public string ReciveBaseDirectory { get; set; }

        public string History { get; set; }

        public bool HaveToSendMail { get; set; }

        public int LCID { get; set; }

        public FileTransferWorkTask()
        {

        }

        public FileTransferWorkTask(string id):base("",id)
        {
            this.CraetedDate = DateTime.UtcNow;
            this.LCID = Thread.CurrentThread.CurrentCulture.LCID;

        }

        public string[] GetFiles()
        {
            return Files.Split('\\');
        }

        public void SetFiles(IEnumerable<string> files)
        {
            Files = string .Join("\\",files);
        }

        public string[] GetToUser()
        {
            if (To == null) return new string[]{};
            return To.Split(';').Select(x => x.Trim()).Where(x => !string.IsNullOrEmpty(x)).ToArray();
        }

        public string[] GetDownloadedUser()
        {
            if (DownloadedUsers == null) return new string[] { };
            return DownloadedUsers.Split(';').Select(x => x.Trim()).Where(x => !string.IsNullOrEmpty(x)).ToArray();
        }

        public void AddDownloadedUser(string user)
        {
            if (string.IsNullOrEmpty(this.DownloadedUsers))
            {
                this.DownloadedUsers =  user;
            }
            else
            {
                if (!GetDownloadedUser().Contains(user))
                {
                    this.DownloadedUsers += "\\" + user;
                }
            }
        }

        public IEnumerable<string> GetValidUser()
        {
            return (new string[] { From }).Concat(GetToUser());
        }

        public bool IsValidUser(string name)
        {
            foreach(var user in GetValidUser())
            {
                var mailAddress = new MailAddress(user);
                if (string.Equals(name, mailAddress.Address)) return true;
            }
            return false;
        }

        public string GetPassword()
        {
            return Password;
        }

        public string GetImpersonationName()
        {
            return From;
        }

        public string Id
        {
            get { return this.RowKey; }
        }

        public DateTime GetNextReminderDate()
        {
            if (!ReminderTimes.HasValue || ReminderTimes.Value <= 0) return DateTime.MaxValue;
            if (ReminderCount >= ReminderTimes) return DateTime.MaxValue;
            var spanUnit = this.Expired.Subtract(this.CraetedDate).TotalMinutes / (ReminderTimes.Value+1);
            var nextSpan = spanUnit * (this.ReminderCount + 1);
            return this.CraetedDate.AddMinutes(nextSpan);
        }

        public IEnumerable<string> GetReminderToUser()
        {
            var list = new List<string>();
            var downloaedusers = GetDownloadedUser();
            return GetToUser().Except(downloaedusers);
        }


        public int GetLCID()
        {
            return this.LCID;
        }
    }
}
